MySQLインストール

概要:MySQLをインストールする。DBの存在するディレクトリーをデフォルト以外の場所にした例を示す。また、テーブルにはINNODBを使用する設定とした。

1. MySQLインストール

# yum -y install mysql-server ← mysql-serverインストール
   :
   :
Dependencies Resolved

==========================================================================================
 Package                  Arch           Version                     Repository      Size
==========================================================================================
Installing:
 mysql-server             i386           5.0.77-4.el5_4.2            base           9.8 M
Installing for dependencies:
 mysql                    i386           5.0.77-4.el5_4.2            base           4.8 M
 perl-DBD-MySQL           i386           3.0007-2.el5                base           148 k
 perl-DBI                 i386           1.52-2.el5                  base           600 k
   :
   :

2. MySQL設定
データベース用のディレクトリーをデフォルト以外に設定する場合、そのディレクトリーを作成する。また、SELinuxのオプションを変更する。mysqld_disable_transの設定を参照。

# mkdir -p /apli/db/mysql/data
# chown mysql. /apli/db/mysql/data
# setsebool -P mysqld_disable_trans=1

コンフィギュレーション設定サンプルのファイルは、/usr/share/mysql にある。
下表はそれぞれのファイルの使用用途である。下表はMySQLをメインに使う場合の設定なので、他のサービスと併用する場合は、ランクを下げた設定とする。

ファイル名使用用途
my-small.cnf64MB以下のメモリを搭載したPC
my-medium.cnf128MB以下のメモリを搭載したPC
my-large.cnf512MB以下のメモリを搭載したPC
my-huge.cnf1GB~2GB以下のメモリを搭載したPC
my-innodb-heavy-4G.cnf4GBのメモリとInnoDBで作成されたデータベースによって構築されたPC

このファイルを目的に合わせて、/etc/my.cnf として保存する。

# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf ← my-medium.cnfの例の場合
# vi /etc/my.cnf ← 追加のMySQL設定のためにファイルを編集する。
[client] ← clientセクション
default-character-set = utf8 ← 追加(MySQLクライアントの文字コードをUTF-8にする)

[mysqld] ← mysqldセクション
datadir=/apli/db/mysql/data/ ← データベースのファイルの場所を/var/lib/mysql以外に
設定する場合追加する。(デフォルトは/var/lib/mysqlである。参照。)
skip-innodb ← INNODBを使用しない場合追加する。今回このオプションは追加しない。
INNODBを使用する場合は以下のようにinnodb_xxxxのオプションのコメントを外し、稼動状況に
より値を チューニングする。
# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /apli/db/mysql/data/ ← 上記datadirを設定した場合その値に合わせる。
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /apli/db/mysql/data/ ← 上記datadirを設定した場合その値に合わせる。
innodb_log_arch_dir = /apli/db/mysql/data/ ← 上記datadirを設定した場合その値に合わせる。
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 10 ← 10秒に設定
innodb_file_per_table ← innodbのファイルをテーブル毎に作成する設定を追加
default-character-set = utf8 ← 追加(MySQLサーバーの文字コードをUTF-8にする)
 
[mysql] ← mysqlセクション
default-character-set = utf8 ← 追加(MySQLコマンドラインツールの文字コードをUTF-8にする)

注:/etc/rc.d/init.d/mysqldはmy.cnf中にdatadirの設定が無い場合datadirを/var/lib/mysqlに設定する。また、$datadir/mysqlのディレクトリーが無い場合、データベースの初期化(/usr/bin/mysql_install_db)を起動する。

3. MySQL起動

# /etc/rc.d/init.d/mysqld start ← MySQL起動
MySQL データベースを初期化中:  Installing MySQL system tables...
100307 16:24:37 [Warning] option 'max_join_size': unsigned value 18446744073709551615
 adjusted to 4294967295
100307 16:24:37 [Warning] option 'max_join_size': unsigned value 18446744073709551615
 adjusted to 4294967295
OK
Filling help tables...
100307 16:24:37 [Warning] option 'max_join_size': unsigned value 18446744073709551615
 adjusted to 4294967295
100307 16:24:37 [Warning] option 'max_join_size': unsigned value 18446744073709551615
 adjusted to 4294967295
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h centos54.com password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                       [  OK  ]
MySQL を起動中:                                        [  OK  ]
# chkconfig mysqld on ← MySQL自動起動設定
# chkconfig --list mysqld ← MySQL自動起動設定確認
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
  ← ランレベル2~5のonを確認

4. MySQLのrootパスワード設定

# /usr/bin/mysqladmin -u root password 'passwd' ← パスワードの設定
又は
# /usr/bin/mysqladmin -u root -h host.local.com password 'passwd'
  • 注:パスワード設定後にmysqladminを使用する場合は、-pオプションを使用してパスワードの入力が必要である。mysqladmin version、mysqladmin pingなども同様である。
    # /usr/bin/mysqladmin -u root -ppasswd password 'new_passwd'
    又は
    # /usr/bin/mysqladmin -u root password 'new_passwd' -p
    Enter password: ← 古いパスワードを入力する。
    
  • pオプションが無い場合は、以下のようなエラーとなる。
    #  /usr/bin/mysqladmin -u root password 'new_passwd'
    /usr/bin/mysqladmin: connect to server at 'localhost' failed
    error: 'Access denied for user 'root'@'localhost' (using password: NO)'
    
    # mysql -u root -ppasswd mysql ← DB mysqlに接続
    又は
    # mysql -u root -h host.local.com -ppasswd mysql
    mysql> SELECT host,user,password from user;
    +----------------+------+------------------+
    | host           | user | password         |
    +----------------+------+------------------+
    | localhost      | root | 68d4f47c49a579c9 |
    | host.local.com | root |                  | ← 自ホストのrootにパスワードが未設定
    | 127.0.0.1      | root |                  | ← 127.0.0.1のrootにパスワードが未設定
    | localhost      |      |                  |
    | host.local.com |      |                  |
    +----------------+------+------------------+
    5 rows in set (0.00 sec)
    
    mysql> set password for root@'host.local.com'=password('passwd');
    Query OK, 0 rows affected (0.00 sec)
    mysql> set password for root@127.0.0.1=password('passwd');
    Query OK, 0 rows affected (0.00 sec)
    mysql> SELECT host,user,password from user;
    +----------------+------+------------------+
    | host           | user | password         |
    +----------------+------+------------------+
    | localhost      | root | 68d4f47c49a579c9 |
    | host.local.com | root | 68d4f47c49a579c9 |
    | 127.0.0.1      | root | 68d4f47c49a579c9 |
    | localhost      |      |                  |
    | host.local.com |      |                  |
    +----------------+------+------------------+
    5 rows in set (0.00 sec)
    

5. 不要ユーザの削除
ユーザ名無しの匿名ユーザを削除する。

mysql> DELETE FROM user where user='';
Query OK, 2 rows affected (0.00 sec)
mysql> SELECT host,user,password from user;
+----------------+------+------------------+
| host           | user | password         |
+----------------+------+------------------+
| localhost      | root | 68d4f47c49a579c9 |
| host.local.com | root | 68d4f47c49a579c9 |
| 127.0.0.1      | root | 68d4f47c49a579c9 |
+----------------+------+------------------+
3 rows in set (0.00 sec)

6. 不要データベースの削除 デフォルトでtestという空のデータベースが登録されているが、不要なので削除する。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| test               | 
+--------------------+
3 rows in set (0.00 sec)

mysql> drop database test; ← testデータベースの削除
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
+--------------------+
2 rows in set (0.00 sec)

7. 文字コードの確認

mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

最終更新のRSS
Last-modified: 2014-03-11 (火) 01:59:57 (3700d)